package org.liang.agile.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DelayedTyper implements Command {

    private static final Logger LOGGER = LoggerFactory.getLogger(DelayedTyper.class);

    private long delay;

    private String val;

    private static boolean stop;

    private static ActiveObjectEngine engine = new ActiveObjectEngine();

    public DelayedTyper(long delay, String val) {
        super();
        this.delay = delay;
        this.val = val;
    }

    @Override
    public void execute() {
        LOGGER.info(val);
        if (!stop) {
            delayAndRepeat();
        }
    }

    private void delayAndRepeat(){
        engine.addCommand(new SleepCommand(delay, engine, this));
    }
    
    public static void main(String[] args) {
        engine.addCommand(new DelayedTyper(1000, "1"));
        engine.addCommand(new DelayedTyper(3000, "3"));
        engine.addCommand(new DelayedTyper(5000, "5"));
        engine.addCommand(new DelayedTyper(7000, "7"));
        
        Command stopCommand = new Command(){
            @Override
            public void execute() {
                stop = true;
            }
        };
        
        engine.addCommand(new SleepCommand(20000, engine, stopCommand));
        engine.run();
    }
    
}
